package com.ymatou.iapi.trading.operate.testcase;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.operate.parameter.req.BatchDispatchedBean;
import com.ymatou.iapi.trading.operate.parameter.req.OrderLogisticsInfoPo;
import com.ymatou.iapi.trading.operate.service.BatchDispatchedCall;
import com.ymatou.iapi.trading.operate.service.LogisticsDeliveryService;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.common.entitie.DispatchedInfo;
import com.ymttest.common.enums.BillTypeEnum;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;

/**
 * 批量发货 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_BatchDispatched {
	private static BatchDispatchedBean batchdispatchedbean;
	private static BatchDispatchedCall batchdispatchedcall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("批量发货");
	}

	@Before
	public void caseUp() {

		batchdispatchedbean = new BatchDispatchedBean();
		batchdispatchedcall = new BatchDispatchedCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(batchdispatchedcall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BatchDispatched_001() {
		Logger.start(true, "批量发货成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			ArrayList<DispatchedInfo> dispatchedList = new ArrayList<DispatchedInfo>();
			Map map = new OrderCallService().createBatchM2cOrder(3);
			JSONArray orderIdList = (JSONArray) map.get("orderIds");
			int sellerId = (Integer) map.get("sellerId");
			int userId = (Integer) map.get("userId");
			int tradingId = (Integer) map.get("tradingId");

			BigDecimal totalPrice = new OrderCallService()
					.getNeedPrePayAmountOfCashBymainOrderId(tradingId);// 支付金额

			for (int i = 0; i < orderIdList.length(); i++) {
				// 根据交易号支付，只需处理一次即可
				while (i == 0) {
					new OrderCallService().orderPayRequest(userId, totalPrice,
							tradingId);
					break;
				}
				int orderId = orderIdList.getInt(i);
				// 接单
				new OrderCallService().acceptOrder(orderId, sellerId);
				// list
				DispatchedInfo info = new DispatchedInfo();
				// 订单类型
				info.setBillType(BillTypeEnum.YMTAPI.getValue());
				// 物流单号
				String deliverSummary = TestDataManager.randomStr(10);
				info.setDeliverSummary(deliverSummary);
				info.setDeliverType("0");
				info.setOrderId(orderId);
				info.setProvider("洋码头官方合作物流（贝海国际速递）");
				info.setIsShippedByXlobo(true);

				BigDecimal weight = new BigDecimal(10);
				info.setWeight(weight);
				dispatchedList.add(info);
			}
			Logger.comment("前置数据准备结束");
			// 准备参数
			batchdispatchedbean.setDispatchedList(dispatchedList);
			batchdispatchedcall.setData(batchdispatchedbean);
			batchdispatchedcall.callService();
			Logger.verifyEquals(ECode.SUCCESS, batchdispatchedcall.getCode(),
					"验证code");
			// 数据库验证
			DefaultWapper database = new DefaultWapper();

			for (DispatchedInfo dispatchedInfo : batchdispatchedbean
					.getDispatchedList()) {

				int orderId = dispatchedInfo.getOrderId();

				HashMap<String, Integer> m = new HashMap<String, Integer>();
				m.put("OrderId", orderId);
				m.put("TradingStatus", 3);
				m.put("TraddingId", orderId);
				m.put("SellerId", sellerId);
				Logger.verifyEquals(1, database.selectOrder(m).size(),
						"验证订单表记录");
				Logger.verifyEquals(1, database.selectTradingStatus(m).size(),
						"验证交易表记录");

				OrderLogisticsInfoPo orderLogisticsInfoPo = LogisticsDeliveryService
						.getLogisticsDeliveryByOrderId(orderId).get(0);

				Logger.verifyEquals(dispatchedInfo.getDeliverSummary(),
						orderLogisticsInfoPo.getBillCode(),
						"验证Ymt_OrderLogisticsInfo 表 BillCode");
				Logger.verifyEquals(dispatchedInfo.getProvider(),
						orderLogisticsInfoPo.getCompany(),
						"验证Ymt_OrderLogisticsInfo 表 Company");

				Logger.verifyEquals(dispatchedInfo.getBillType(),
						orderLogisticsInfoPo.getDeliverType(),
						"验证Ymt_OrderLogisticsInfo 表 deliverType");

				DataUtil.verifyDecimalEquals(dispatchedInfo.getWeight(),
						orderLogisticsInfoPo.getWeight(),
						"验证Ymt_OrderLogisticsInfo 表 Weight");

				Logger.verifyEquals(dispatchedInfo.getDeliverType().equals("0")
						? false
						: true, orderLogisticsInfoPo.getChinaDeliver(),
						"验证Ymt_OrderLogisticsInfo 表 chinaDeliver");

				Logger.verifyEquals(dispatchedInfo.getIsShippedByXlobo(),
						orderLogisticsInfoPo.getOfficialLogistics(),
						"验证Ymt_OrderLogisticsInfo 表 officialLogistics");

				Logger.verifyEquals(false, orderLogisticsInfoPo.getDeleted(),
						"验证Ymt_OrderLogisticsInfo 表 deleted");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BatchDispatched_002() {
		Logger.start(false, "订单不存在");
		try {
			ArrayList<DispatchedInfo> dispatchedList = new ArrayList<DispatchedInfo>();
			// list
			int orderId = 123;
			int sellerId = 288484;
			DispatchedInfo info = new DispatchedInfo();
			// 订单类型
			info.setBillType(BillTypeEnum.YMTAPI.getValue());
			// 物流单号
			String deliverSummary = TestDataManager.randomStr(10);
			info.setDeliverSummary(deliverSummary);
			info.setDeliverType("0");
			info.setOrderId(orderId);
			info.setProvider("洋码头官方合作物流（贝海国际速递）");

			BigDecimal weight = new BigDecimal(10);
			info.setWeight(weight);
			dispatchedList.add(info);
			// 准备参数
			batchdispatchedbean.setDispatchedList(dispatchedList);
			batchdispatchedcall.setData(batchdispatchedbean);
			batchdispatchedcall.callService();
			Logger.verifyEquals(ECode.FAILED201, batchdispatchedcall.getCode(),
					"验证Code");
			Logger.verifyIsContains(ECode.FAILED201_MSG_11,
					batchdispatchedcall.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BatchDispatched_003() {
		Logger.start(true, "当前订单状态不能发货");
		try {
			Logger.comment("前置数据准备");
			ArrayList<DispatchedInfo> dispatchedList = new ArrayList<DispatchedInfo>();
			// 创建订单
			Map map = new OrderCallService().createBatchM2cOrder(3);
			JSONArray orderIdList = (JSONArray) map.get("orderIds");
			int sellerId = (Integer) map.get("sellerId");
			int userId = (Integer) map.get("userId");
			int tradingId = (Integer) map.get("tradingId");
			// 保留两位小数
			BigDecimal totalPrice = (BigDecimal) map.get("totalPrice");// 支付金额
			for (int i = 0; i < orderIdList.length(); i++) {
				// 根据交易号支付，只需处理一次即可
				while (i == 0) {
					new OrderCallService().orderPayRequest(userId, totalPrice,
							tradingId);
					break;
				}
				int orderId = orderIdList.getInt(i);
				// 接单
				// new OrderCallService().acceptOrder(OrderId, SellerId);
				// list
				DispatchedInfo info = new DispatchedInfo();
				// 订单类型
				info.setBillType(BillTypeEnum.YMTAPI.getValue());
				// 物流单号
				String deliverSummary = TestDataManager.randomStr(10);
				info.setDeliverSummary(deliverSummary);
				info.setDeliverType("0");
				info.setOrderId(orderId);
				info.setProvider("洋码头官方合作物流（贝海国际速递）");

				BigDecimal weight = new BigDecimal(10);
				info.setWeight(weight);
				dispatchedList.add(info);
			}
			// 准备参数
			Logger.comment("前置数据准备结束");
			batchdispatchedbean.setDispatchedList(dispatchedList);
			batchdispatchedcall.setData(batchdispatchedbean);
			batchdispatchedcall.callService();
			Logger.verifyEquals(ECode.FAILED201, batchdispatchedcall.getCode(),
					"验证Code");
			Logger.verifyEquals(
					true,
					batchdispatchedcall.getMsg().contains(
							ECode.FAILED201_MSG_28), "验证Msg包含信息:"
							+ ECode.FAILED201_MSG_28);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BatchDispatched_004() {
		Logger.start(true, "发货信息列表信息为null");
		try {
			ArrayList<DispatchedInfo> dispatchedList = new ArrayList<DispatchedInfo>();
			batchdispatchedbean.setDispatchedList(null);
			batchdispatchedcall.setData(batchdispatchedbean);
			batchdispatchedcall.callService();
			Logger.verifyEquals(ECode.ERROR400, batchdispatchedcall.getCode(),
					"验证Code");
			Logger.verifyIsContains("发货信息列表不能为空", batchdispatchedcall.getMsg(),
					"验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BatchDispatched_005() {
		Logger.start(true, "订单号不存在");
		try {
			ArrayList<DispatchedInfo> dispatchedList = new ArrayList<DispatchedInfo>();
			// list
			DispatchedInfo info = new DispatchedInfo();
			// 订单类型
			info.setBillType(BillTypeEnum.YMTAPI.getValue());
			// 物流单号
			String deliverSummary = TestDataManager.randomStr(10);
			info.setDeliverSummary(deliverSummary);
			info.setDeliverType("0");
			info.setOrderId(1111);
			info.setProvider("洋码头官方合作物流（贝海国际速递）");

			BigDecimal weight = new BigDecimal(10);
			info.setWeight(weight);
			dispatchedList.add(info);
			// 准备参数
			batchdispatchedbean.setDispatchedList(dispatchedList);
			batchdispatchedcall.setData(batchdispatchedbean);
			batchdispatchedcall.callService();
			Logger.verifyEquals(ECode.FAILED201, batchdispatchedcall.getCode(),
					"验证Code");
			Logger.verifyEquals(
					ECode.FAILED201_MSG_54.replace(
							"orderid",
							String.valueOf(batchdispatchedbean
									.getDispatchedList().get(0).getOrderId())),
					batchdispatchedcall.getMsg(), "验证Code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BatchDispatched_006() {
		Logger.start(true, "当前订单为退货退款中,批量发货失败");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			ArrayList<DispatchedInfo> dispatchedList = new ArrayList<DispatchedInfo>();
			Map map = new OrderCallService().createBatchM2cOrder(3);
			JSONArray orderIdList = (JSONArray) map.get("orderIds");
			int sellerId = (Integer) map.get("sellerId");
			int userId = (Integer) map.get("userId");
			int tradingId = (Integer) map.get("tradingId");
			// 保留两位小数
			BigDecimal totalPrice = (BigDecimal) map.get("totalPrice");// 支付金额
			for (int i = 0; i < orderIdList.length(); i++) {
				// 根据交易号支付，只需处理一次即可
				while (i == 0) {
					new OrderCallService().orderPayRequest(userId, totalPrice,
							tradingId);
					break;
				}
				int orderId = orderIdList.getInt(i);
				// 退货退款同步
				new OrderCallService().applySalesRefundRequestInJava(orderId); // list
				DispatchedInfo info = new DispatchedInfo();
				// 订单类型
				info.setBillType(BillTypeEnum.YMTAPI.getValue());
				// 物流单号
				String deliverSummary = TestDataManager.randomStr(10);
				info.setDeliverSummary(deliverSummary);
				info.setDeliverType("0");
				info.setOrderId(orderId);
				info.setProvider("洋码头官方合作物流（贝海国际速递）");

				BigDecimal weight = BigDecimal.TEN;
				info.setWeight(weight);
				dispatchedList.add(info);
			}
			Logger.comment("前置数据准备结束");
			// 准备参数
			batchdispatchedbean.setDispatchedList(dispatchedList);
			batchdispatchedcall.setData(batchdispatchedbean);
			batchdispatchedcall.callService();
			Logger.verifyEquals(ECode.FAILED201, batchdispatchedcall.getCode(),
					"验证Code");
			Logger.verifyEquals(ECode.FAILED201_MSG_28,
					batchdispatchedcall.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
